/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Presentation;

import BussinesLogic.*;
import com.toedter.calendar.JDateChooser;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class GUILogistica extends javax.swing.JFrame {

    String idEvento;
    private JPanel panelAux;
    
    public GUILogistica(String idEvento) {
        this.idEvento = idEvento;
        panelAux = new JPanel();
        initComponents();
        this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelPrincipal = new javax.swing.JTabbedPane();
        panelColaboradores = new javax.swing.JPanel();
        comboColaboradores = new javax.swing.JComboBox();
        panelAuxColab = new javax.swing.JPanel();
        panelEspacios = new javax.swing.JPanel();
        comboEspaDepor = new javax.swing.JComboBox();
        panelAuxEsp = new javax.swing.JPanel();
        panelCompetencias = new javax.swing.JPanel();
        comboCompetencias = new javax.swing.JComboBox();
        panelAuxComp = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("JOGOS2016 - Director de Logistica");

        panelPrincipal.setOpaque(true);

        comboColaboradores.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Crear", "Modificar", "Consultar", "Eliminar" }));
        comboColaboradores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboColaboradoresActionPerformed(evt);
            }
        });

        panelAuxColab.setOpaque(false);

        javax.swing.GroupLayout panelAuxColabLayout = new javax.swing.GroupLayout(panelAuxColab);
        panelAuxColab.setLayout(panelAuxColabLayout);
        panelAuxColabLayout.setHorizontalGroup(
            panelAuxColabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panelAuxColabLayout.setVerticalGroup(
            panelAuxColabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 290, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout panelColaboradoresLayout = new javax.swing.GroupLayout(panelColaboradores);
        panelColaboradores.setLayout(panelColaboradoresLayout);
        panelColaboradoresLayout.setHorizontalGroup(
            panelColaboradoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelAuxColab, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(panelColaboradoresLayout.createSequentialGroup()
                .addGap(159, 159, 159)
                .addComponent(comboColaboradores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(170, Short.MAX_VALUE))
        );
        panelColaboradoresLayout.setVerticalGroup(
            panelColaboradoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelColaboradoresLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comboColaboradores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelAuxColab, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelPrincipal.addTab("Colaboradores", panelColaboradores);

        comboEspaDepor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Registrar", "Modificar", "Consultar", "Eliminar" }));
        comboEspaDepor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboEspaDeporActionPerformed(evt);
            }
        });

        panelAuxEsp.setOpaque(false);

        javax.swing.GroupLayout panelAuxEspLayout = new javax.swing.GroupLayout(panelAuxEsp);
        panelAuxEsp.setLayout(panelAuxEspLayout);
        panelAuxEspLayout.setHorizontalGroup(
            panelAuxEspLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panelAuxEspLayout.setVerticalGroup(
            panelAuxEspLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 140, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout panelEspaciosLayout = new javax.swing.GroupLayout(panelEspacios);
        panelEspacios.setLayout(panelEspaciosLayout);
        panelEspaciosLayout.setHorizontalGroup(
            panelEspaciosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelEspaciosLayout.createSequentialGroup()
                .addContainerGap(166, Short.MAX_VALUE)
                .addComponent(comboEspaDepor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(163, 163, 163))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelEspaciosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelAuxEsp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        panelEspaciosLayout.setVerticalGroup(
            panelEspaciosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelEspaciosLayout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(comboEspaDepor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(panelAuxEsp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(124, Short.MAX_VALUE))
        );

        panelPrincipal.addTab("Espacios Deportivos", panelEspacios);

        panelCompetencias.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        comboCompetencias.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Crear", "Modificar", "Consultar", "Eliminar" }));
        comboCompetencias.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCompetenciasActionPerformed(evt);
            }
        });
        panelCompetencias.add(comboCompetencias, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 20, -1, -1));

        panelAuxComp.setOpaque(false);

        javax.swing.GroupLayout panelAuxCompLayout = new javax.swing.GroupLayout(panelAuxComp);
        panelAuxComp.setLayout(panelAuxCompLayout);
        panelAuxCompLayout.setHorizontalGroup(
            panelAuxCompLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 330, Short.MAX_VALUE)
        );
        panelAuxCompLayout.setVerticalGroup(
            panelAuxCompLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 270, Short.MAX_VALUE)
        );

        panelCompetencias.add(panelAuxComp, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 50, 330, 270));

        panelPrincipal.addTab("Competencias", panelCompetencias);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelPrincipal)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelPrincipal)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboCompetenciasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCompetenciasActionPerformed
        // TODO add your handling code here:
        int itemSelected = comboCompetencias.getSelectedIndex();

        switch(itemSelected){
            case 0:
            crearCompetenciaGUI();
            break;
            default:
            System.out.println(itemSelected);
        }
    }//GEN-LAST:event_comboCompetenciasActionPerformed

    private void comboColaboradoresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboColaboradoresActionPerformed
        // TODO add your handling code here:
        int itemSelected = comboColaboradores.getSelectedIndex();

        switch(itemSelected){
            case 0:
            GUIRegColaborador colaborador = new GUIRegColaborador(idEvento);
            break;
            default:
            System.out.println(itemSelected);
        }
    }//GEN-LAST:event_comboColaboradoresActionPerformed

    private void comboEspaDeporActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboEspaDeporActionPerformed
        // TODO add your handling code here:
        int itemSelected = this.comboEspaDepor.getSelectedIndex();

        switch(itemSelected){
            case 0:
            registrarEspacioDeportivoGUI();
            break;
            default:
            System.out.println(itemSelected);
        }
    }//GEN-LAST:event_comboEspaDeporActionPerformed

    private void registrarEspacioDeportivoGUI(){
        System.out.println("CREAR ESPACIO PRESIONADO");
        
        this.panelAuxEsp.remove(panelAux);
        this.panelAuxComp.remove(panelAux);
        
        JPanel panel = new JPanel();        
        panel.setLayout(new java.awt.GridLayout(5,2));
        
        JTextField campoNombre = new JTextField(5);
        JTextField campoCap = new JTextField();
        JTextField campoDireccion = new JTextField();
        JButton botonReg = new JButton("REGISTRAR");
        botonReg.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                registrarEspacioDeportivo(campoNombre.getText(), campoCap.getText(), campoDireccion.getText());
            }
        });
        
        panel.add(new JLabel("REGISTRAR ESPACIO DEPORTIVO"));
        panel.add(new JLabel(""));
        panel.add(new JLabel("NOMBRE"));
        panel.add(campoNombre);
        panel.add(new JLabel("CAPACIDAD"));
        panel.add(campoCap);
        panel.add(new JLabel("DIRECCION"));
        panel.add(campoDireccion);
        panel.add(new JLabel(""));
        panel.add(botonReg);
        
        panelAux = panel;
        this.panelAuxEsp.setLayout(new java.awt.GridLayout(1,1));
        this.panelAuxEsp.add(panelAux);
        
        this.revalidate();
        this.repaint();   
    }
    
    private void registrarEspacioDeportivo(String nombre, String cap, String direccion){
        
        if(nombre.isEmpty() || cap.isEmpty() || direccion.isEmpty()){
            JOptionPane.showMessageDialog(this,"Por favor diligenciar todos los campos del formulario"
                    + " y elegir un logo para el evento","JOGOS2016 - Error", JOptionPane.ERROR_MESSAGE);
        }
        else{
            BL_EspacioDeportivo bl_escenario = new BL_EspacioDeportivo();
            Request rqst = bl_escenario.registrarEspacioDeportivo(nombre, cap, direccion);
            
            JOptionPane.showMessageDialog(this,rqst.getMessage(),
                    "JOGOS2016 - Mensaje",JOptionPane.INFORMATION_MESSAGE);

            if (rqst.getTransac()) {
                this.panelAuxEsp.remove(panelAux);
                this.revalidate();
                this.repaint();
            }
            
        }
    }
    
    private void crearCompetenciaGUI(){
        System.out.println("CREAR COMPETENCIA PRESIONADO");
        
        this.panelAuxComp.remove(panelAux);
        this.panelAuxEsp.remove(panelAux);
        
        BL_Disciplina bl_disciplina = new BL_Disciplina();
        BL_EspacioDeportivo bl_escenario = new BL_EspacioDeportivo();
        Vector vDisc = (Vector) bl_disciplina.consultarDisciplinaPorEvento(idEvento).getObject();
        Vector vEsp = (Vector) bl_escenario.consultarEspaciosDeportivos().getObject();
        
        JPanel panel = new JPanel();        
        panel.setLayout(new java.awt.GridLayout(10,2));
        
        JTextField campoNombre = new JTextField();
        JComboBox campoDisciplina = new JComboBox(vDisc);
        JTextField campoDescripcion = new JTextField();
        JDateChooser calFecha = new JDateChooser();
        
        JPanel pHora = new JPanel();
        pHora.setLayout(new java.awt.GridLayout(1,3));
        JComboBox campoHora = new JComboBox(generarSecuencia(0,24));
        campoHora.addItem("HH");
        campoHora.setSelectedItem("HH");
        JComboBox campoMin = new JComboBox(generarSecuencia(0,59));
        campoMin.addItem("MM");
        campoMin.setSelectedItem("MM");
        pHora.add(campoHora);
        pHora.add(campoMin);
        pHora.add(new JLabel(""));
        
        JTextField campoJugFase1 = new JTextField();
        JTextField campoJugFase2 = new JTextField();
        JComboBox campoEspacio = new JComboBox(vEsp);
        
        JButton botonReg = new JButton("CREAR");
        botonReg.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String hora = campoHora.getSelectedItem().toString() + ":" + campoMin.getSelectedItem().toString();
                crearCompetencia(campoNombre.getText(), campoDisciplina.getSelectedItem().toString(),
                        campoDescripcion.getText(),calFecha.getDate(), hora, campoJugFase1.getText(),
                        campoJugFase2.getText(),campoEspacio.getSelectedItem().toString());
                
            }
        });
        
        panel.add(new JLabel("CREAR COMPETENCIA"));
        panel.add(new JLabel("DEPORTIVA"));
        panel.add(new JLabel("NOMBRE"));
        panel.add(campoNombre);
        panel.add(new JLabel("DISCIPLINA"));
        panel.add(campoDisciplina);
        panel.add(new JLabel("DESCRIPCION"));
        panel.add(campoDescripcion);
        panel.add(new JLabel("FECHA"));
        panel.add(calFecha);
        panel.add(new JLabel("HORA"));
        panel.add(pHora);
        panel.add(new JLabel("# JUG Fase Clasificatoria"));
        panel.add(campoJugFase1);
        panel.add(new JLabel("# JUG Fase Eliminatoria"));
        panel.add(campoJugFase2);
        panel.add(new JLabel("ESPACIO DEPORTIVO"));
        panel.add(campoEspacio);
        panel.add(new JLabel(""));
        panel.add(botonReg);
        
        panelAux = panel;
        this.panelAuxComp.setLayout(new java.awt.GridLayout(1,1));
        this.panelAuxComp.add(panelAux);
        
        this.revalidate();
        this.repaint();   
    }
    
    private void crearCompetencia(String nombre, String disciplina, String descripcion,Date fecha, String hora,
            String fase1, String fase2, String escenario){
        
        if(nombre.isEmpty() || disciplina.isEmpty() || descripcion.isEmpty() || fecha==null || hora.isEmpty() ||
                fase1.isEmpty() || fase2.isEmpty() || escenario.isEmpty()){
            JOptionPane.showMessageDialog(this,"Por favor diligenciar todos los campos del formulario"
                    + " y elegir un logo para el evento","JOGOS2016 - Error", JOptionPane.ERROR_MESSAGE);
        }
        else{
            BL_Competencia bl_competencia = new BL_Competencia();
            Request rqst = bl_competencia.registrarCompetencia(nombre, disciplina, descripcion, fecha, hora, fase1,
                    fase2, escenario);
            
            JOptionPane.showMessageDialog(this,rqst.getMessage(),
                    "JOGOS2016 - Mensaje",JOptionPane.INFORMATION_MESSAGE);

            if (rqst.getTransac()) {
                this.panelAuxComp.remove(panelAux);
                this.revalidate();
                this.repaint();
            }
        }
    }
    
    private Vector generarSecuencia(int inicio, int fin) {

        Vector array = new Vector(0,0);
        
        for(;inicio<=fin;inicio++){
            String obj = "";
            if(inicio<10) obj="0"+inicio;
            else obj=""+inicio;
            array.add(obj);
        }
        return array;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUILogistica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUILogistica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUILogistica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUILogistica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUILogistica("1-8-2014-2-9-2014").setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboColaboradores;
    private javax.swing.JComboBox comboCompetencias;
    private javax.swing.JComboBox comboEspaDepor;
    private javax.swing.JPanel panelAuxColab;
    private javax.swing.JPanel panelAuxComp;
    private javax.swing.JPanel panelAuxEsp;
    private javax.swing.JPanel panelColaboradores;
    private javax.swing.JPanel panelCompetencias;
    private javax.swing.JPanel panelEspacios;
    private javax.swing.JTabbedPane panelPrincipal;
    // End of variables declaration//GEN-END:variables
}
